<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <span class="search-title">目前归属</span>
      <el-select class="filter-item" style="width: 130px" @change="getList" v-model="listQuery.uid" placeholder="目前归属">
        <el-option v-for="item in arrs" :key="item.uid" :label="item.truename" :value="item.uid">
        </el-option>
      </el-select>
      <el-button class="filter-item" style="float:right" @click="getOrder" type="primary" icon="el-icon-refresh">重新分配</el-button>
    </div>
    <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="订单初审" name="first" v-if="per === '1' || per === '3'">
        <component :is="allotList" :list="list" :listLoading="listLoading" v-on:has-order="hasOrder"></component>
      </el-tab-pane>
      <el-tab-pane label="订单复审" name="second" v-if="per === '2' || per === '3'">
        <component :is="allotList" :list="list" :listLoading="listLoading" v-on:has-order="hasOrder"></component>
      </el-tab-pane>
    </el-tabs>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
        :page-sizes="[5,10,20,30, 50]" :page-size="listQuery['per-page']" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog width="300px" :title="'请选择指派人'" :visible.sync="dialogFormVisible" @close="resetDio">
      <el-form :rules="rules" status-icon ref="dataForm" :model="params" label-position="left" label-width="0px" style='text-align:center;width: 200px'>
        <el-form-item prop="uid" style='margin:0px auto'>
          <el-select class="filter-item" v-model="params.uid" @change="resetDio" placeholder="请选择指派人">
            <el-option v-for="item in b_arrs" :key="item.uid" :label="item.truename" :value="item.uid">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style='text-align:center'>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="regive">确 认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style scoped>
  .el-select--medium {
    margin-bottom: 0px;
  }
</style>

<script>
import { firstAssignSelect, firstAssignList, firstRegive, secondAssignSelect, secondAssignList, secondRegive } from '@/api/order'
import allotlist from './components/allotlist' // 订单列表页
import { hasOrderPermission } from '@/utils'

export default {
  name: 'complexTable',
  data() {
    // 数据校验
    var checkUid = (rule, value, callback) => {
      if (!value || value === 0) {
        return callback(new Error('请选择指派人'))
      } else {
        callback()
      }
    }
    return {
      allotList: allotlist,
      activeName: 'first',
      tableKey: 0,
      list: null,
      total: null,
      per: '3',
      listLoading: true,
      params: {
        oids: '',
        uid: 0
      },
      listQuery: {
        page: 1,
        'per-page': 20,
        uid: 0
      },
      dialogFormVisible: false,
      arrs: [{
        uid: 0,
        truename: '全部'
      }],
      b_arrs: [{
        uid: 0,
        truename: '未选择'
      }],
      rules: {
        uid: [{ validator: checkUid, trigger: 'blur' }]
      }
    }
  },
  created() {
    // 获取初审复审权限
    this.per = hasOrderPermission()
    if (this.per === '1') {
      this.activeName = 'first'
    } else if (this.per === '2') {
      this.activeName = 'second'
    } else if (this.per === '3') {
      this.activeName = 'first'
    }
    this.getSelect()
  },
  methods: {
    getSelect() {
      if (this.activeName === 'first') {
        firstAssignSelect().then(response => {
          for (const k in response.data) {
            this.arrs[parseInt(k) + 1] = response.data[k]
            this.b_arrs[parseInt(k) + 1] = response.data[k]
          }
          this.getList()
        })
      } else {
        secondAssignSelect().then(response => {
          for (const k in response.data) {
            this.arrs[parseInt(k) + 1] = response.data[k]
            this.b_arrs[parseInt(k) + 1] = response.data[k]
          }
          this.getList()
        })
      }
    },
    getList() {
      this.listLoading = true
      // 判断是待初审还是待复审
      if (this.activeName === 'first') {
        firstAssignList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data._meta.totalCount
          this.listLoading = false
        })
      } else {
        secondAssignList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data._meta.totalCount
          this.listLoading = false
        })
      }
    },
    cancel() { // 取消弹框
      this.dialogFormVisible = false
      this.$refs['dataForm'].clearValidate()
    },
    resetDio() {
      this.$refs['dataForm'].clearValidate()
    },
    regive() { // 重新分配订单
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.activeName === 'first') {
            firstRegive(this.params).then(() => {
              this.dialogFormVisible = false
              this.$message({
                title: '成功',
                message: '重新指派成功',
                type: 'success',
                duration: 2000,
                onClose: () => {
                  this.getList()
                }
              })
            })
          } else {
            secondRegive(this.params).then(() => {
              this.dialogFormVisible = false
              this.$message({
                title: '成功',
                message: '重新指派成功',
                type: 'success',
                duration: 2000,
                onClose: () => {
                  this.getList()
                }
              })
            })
          }
        }
      })
    },
    handleClick() {
      this.resetTemp()
      this.getSelect()
    },
    handleSizeChange(val) {
      this.listQuery['per-page'] = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    hasOrder: function(str) { // 接收子组件传来的订单id
      this.params.oids = str
    },
    getOrder: function() { // 领取订单
      if (this.params.oids === '') {
        this.$alert('请先勾选想要领取的工单', '提示', {
          confirmButtonText: '确定',
          type: 'warning'
        }).then(() => {
        })
      } else {
        // 声明变量
        this.dialogFormVisible = true
      }
    },
    resetTemp() {
      this.listQuery = {
        page: 1,
        'per-page': 10,
        uid: 0
      }
      this.arrs = [{
        uid: 0,
        truename: '全部'
      }]
      this.b_arrs = [{
        uid: 0,
        truename: '未选择'
      }]
    }
  }
}
</script>
